package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bean.AnimalBean;
import bean.UserBean;
import utils.JdbcUtil;

public class AnimalDao {
	
	// 登记动物
	public static void add(String a_title, String a_type, String a_sex, String a_age, String a_location, String a_detail_local,
							String a_contact, String a_phone, String a_desc, String a_pic, String a_auth, String a_status) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		conn = JdbcUtil.conn();
		String sql = "insert into animal_info(a_type, a_sex, a_title, a_age, a_contact, a_phone, a_location, a_detail_loca, a_pic, a_desc, a_auth, a_status) values ('" +a_type + "','" + a_sex + "','" + a_title +"','" + a_age + "','" 
														+ a_contact + "','" + a_phone +"','" + a_location + "','" + a_detail_local + "','"
														+ a_pic +"','" + a_desc+ "','" + a_auth + "','"+ a_status +"');";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.relase(pstmt, conn);
		}
	}
	
	// 查询所有
	public static List<AnimalBean> query() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rSet = null;
		
		List<AnimalBean> animal_list = new ArrayList<>();
		
		conn = JdbcUtil.conn();
		String sql = "select * from animal_info where a_status='0';";
		try {
			pstmt	= conn.prepareStatement(sql);
			rSet = pstmt.executeQuery();
			
			while(rSet.next()) {
				AnimalBean animalBean = new AnimalBean(rSet.getString("a_type"), rSet.getString("a_sex"), rSet.getString("a_title"), rSet.getString("a_age"), 
						rSet.getString("a_contact"), rSet.getString("a_phone"), rSet.getString("a_location"), rSet.getString("a_detail_loca"), 
						rSet.getString("a_pic"), rSet.getString("a_desc"), rSet.getString("a_auth"), rSet.getString("a_status"), rSet.getInt("a_id"));
				animal_list.add(animalBean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return animal_list;
	}

	// 当前用户的列表
	public static List<AnimalBean> query(String a_auth) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rSet = null;
		
		List<AnimalBean> animal_list = new ArrayList<>();
		
		conn = JdbcUtil.conn();
		String sql = "select * from animal_info where a_auth='"+ a_auth +"';";
		try {
			pstmt	= conn.prepareStatement(sql);
			rSet = pstmt.executeQuery();
			
			while(rSet.next()) {
				AnimalBean animalBean = new AnimalBean(rSet.getString("a_type"), rSet.getString("a_sex"), rSet.getString("a_title"), rSet.getString("a_age"), 
						rSet.getString("a_contact"), rSet.getString("a_phone"), rSet.getString("a_location"), rSet.getString("a_detail_loca"), 
						rSet.getString("a_pic"), rSet.getString("a_desc"), rSet.getString("a_auth"), rSet.getString("a_status"), rSet.getInt("a_id"));
				animal_list.add(animalBean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return animal_list;
	}

	public static AnimalBean query(int a_id) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rSet = null;
		
		List<AnimalBean> animal_list = new ArrayList<>();
		
		conn = JdbcUtil.conn();
		String sql = "select * from animal_info where a_id="+ a_id +";";
		try {
			pstmt	= conn.prepareStatement(sql);
			rSet = pstmt.executeQuery();
			
			while(rSet.next()) {
				AnimalBean animalBean = new AnimalBean(rSet.getString("a_type"), rSet.getString("a_sex"), rSet.getString("a_title"), rSet.getString("a_age"), 
						rSet.getString("a_contact"), rSet.getString("a_phone"), rSet.getString("a_location"), rSet.getString("a_detail_loca"), 
						rSet.getString("a_pic"), rSet.getString("a_desc"), rSet.getString("a_auth"), rSet.getString("a_status"), rSet.getInt("a_id"));
				animal_list.add(animalBean);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return animal_list.get(0);
	}

	@SuppressWarnings("resource")
	public static void update(int a_id, String user_phone) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		
		conn = JdbcUtil.conn();
		String sql1 = "update animal_info set a_status='1' where a_id="+ a_id +";";
		String sql2= "insert into adopt_info values("+a_id+", '"+ user_phone +"');";
		try {
			pstmt = conn.prepareStatement(sql1);
			pstmt.executeUpdate(sql1);
			pstmt = conn.prepareStatement(sql2); 
			pstmt.executeUpdate(sql2);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.relase(pstmt, conn);
		}
	}

}
